package com.boby.homecamera.activity

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowInsetsController
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.boby.homecamera.R
import com.boby.homecamera.activity.client.ClientHomeActivity
import com.boby.homecamera.activity.server.InitServerActivity
import com.boby.homecamera.activity.server.ServerHomeActivity
import com.boby.homecamera.config.AppConfig
import com.boby.homecamera.constant.Constant
import com.boby.homecamera.db.ClientDBManager
import com.boby.homecamera.service.MainService
import com.boby.lib_common.extens.setVisible

class LauncherActivity : AppCompatActivity() {

    private val permissions: Array<String>

    init {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            permissions = arrayOf(
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.FOREGROUND_SERVICE,
                Manifest.permission.FOREGROUND_SERVICE_CAMERA,
                Manifest.permission.FOREGROUND_SERVICE_MICROPHONE,
                Manifest.permission.POST_NOTIFICATIONS
            )
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            permissions = arrayOf(
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.FOREGROUND_SERVICE
            )
        } else {
            permissions = arrayOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA)
        }
    }

    private lateinit var  ll_select_model:View
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_launcher)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                window.insetsController?.setSystemBarsAppearance(
                    WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                    WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
                )
            }else{
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
                            View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
            }
        }else{
            // 设置状态栏白底
            window.statusBarColor = Color.WHITE
            // 设置状态栏字体图标为黑色（亮色模式 = false）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }

        ll_select_model = findViewById<View>(R.id.ll_select_model)
        init()
    }

    private fun init(){
        when(AppConfig.deviceModel){ // 0:未选择，1：服务端模式 2：客户端模式
            Constant.DEVICE_MODEL_SERVER -> {
                startServer()
            }
            Constant.DEVICE_MODEL_CLIENT -> {
                startClient()
            }
            else -> {
                selectModel()
            }
        }

    }

    private fun startServer(){

        if(MainService.isRunning){
            startActivity(Intent(this, ServerHomeActivity::class.java))
        }else{
            startActivity(Intent(this, InitServerActivity::class.java))
        }

        finish()
    }

    private fun startClient(){
        if (!hasPermissions(this, *permissions)) {
            ActivityCompat.requestPermissions(this, permissions, 1)
            return
        }

        ClientDBManager.instance.init()
        //登录
        startActivity(Intent(this, ClientHomeActivity::class.java))
//        if(AppConfig.token.isEmpty()){
//
//        }else{
//            //首页
//        }
        finish()

    }

    private fun selectModel(){
        ll_select_model.setVisible(true)
    }

    fun startServer(view: View) {
        AppConfig.deviceModel = Constant.DEVICE_MODEL_SERVER
        startServer()
    }

    fun startClient(view: View) {
        AppConfig.deviceModel = Constant.DEVICE_MODEL_CLIENT
        startClient()
    }

    private fun hasPermissions(context: Context?, vararg permissions: String): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null) {
            for (permission in permissions) {
                if (ActivityCompat.checkSelfPermission(
                        context,
                        permission
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    return false
                }
            }
        }
        return true
    }
}